﻿using AsyncImageLoader;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using MusicFluent.Controls;
using MusicFluent.Models;
using MusicFluent.MusicViews;
using MusicFluent.ViewModels.Pages;
using MusicFluent.Views;

namespace MusicFluent.Pages;

public partial class MyView : PageViewBase
{
    public MyView()
    {
        InitializeComponent();
    }

    protected override async void OnInitialized()
    {
        if (DataContext is not MyViewModel model) return;
        if (model.User.IsVisitor)
        {
            Content = new VisitorControl();
            return;
        }

        AvatarImage.Source = $"{model.User.AvatarUrl}?param=60y60";
        VipImage.Source = $"{model.User.VipUrl}?param=40y60";
        var image = new ImageBrush() { Stretch = Stretch.UniformToFill };
        ImageBrushLoader.SetSource(image, $"{model.User.BackgroundUrl}?param=300y180");
        LikeBorder.Background = image;
        var main = ViewOpera.GetView<MainWindow>(this);
        main?.TaskShow();
        var playlists = await model.UserApp.GetPlaylists(model.User.UserId);
        PlaylistItems.ItemsSource = playlists.Length > 0 ? playlists[1..] : [];
        main?.TaskShow();
        LikeBorder.DataContext = playlists[0];
        var cloudModel = await model.UserApp.GetCloud();
        CloudBar.Value = cloudModel.Size / cloudModel.MaxSize;
        CloudBlock.Text = $"{cloudModel.Size}G / {cloudModel.MaxSize}G , 一共{cloudModel.Count}首歌";
        CloudSongItems.ItemsSource = cloudModel.Songs;
        RecentlyPlayedSongItems.ItemsSource = await model.UserApp.RecordSong();
    }

    private async void YearReportTapped(object? sender, TappedEventArgs e)
    {
        var view = ViewOpera.GetView<MainWindow>(this);
        if (view == null) return;
        view.TaskShow();
        if (DataContext is not MyViewModel model) return;
        var v = new YearReportView() { DataContext = await model.UserApp.YearReport() };
        view.Navigate(v);
        view.TaskShow();
    }

    private void UserClick(object? sender, RoutedEventArgs e)
    {
    }
}